import sys
import hashlib
import requests
import json
from main import *
from function import *
from borrow import *
from back import *
from history import *
from orgnize import *
from PyQt5.QtWidgets import QApplication, QMessageBox
import constance

url = "http://lenjon.top:9090/"


def md5(s):
    h = hashlib.md5()
    h.update(s.encode(encoding='utf-8'))
    return h.hexdigest()


def Login_in(username, password):
    data = {'username': username, 'password': md5(password), 'type': 1}
    post_data = json.dumps(data)
    headers = {
        'Content-Type': "application/json;charset=utf8",
        'Accept': '*/*'
    }
    res = requests.post(url + 'login', data=post_data, headers=headers)
    res_data = json.loads(res.text)
    if res_data['code'] == 200:
        TOKEN = res_data['data']
        constance.set_token(TOKEN)  # TOKEN存入全局变量
        return True, res_data
    else:
        return False, res_data


# 对应功能：借书、还书
def transfer(username, book_id, url_):
    headers = {
        'Content-Type': "application/json;charset=utf8",
        'Accept': '*/*',
        'token': constance.get_token()
    }
    res = requests.get(url + url_ + '?username=' + username + '&bookId=' + book_id, headers=headers)
    res_data = json.loads(res.text)
    print(res_data)
    return res_data['message']


# 对应功能：图书管理(上下架）
def transfer_(book_id, status):
    headers = {
        'Content-Type': "application/json;charset=utf8",
        'Accept': '*/*',
        'token': constance.get_token()
    }
    res = requests.get(url + '/manager/setStatus' + '?bookId=' + book_id + '&status=' + status, headers=headers)
    res_data = json.loads(res.text)
    return res_data['message']


class Controller(object):
    def __init__(self):
        self.m = MPage()
        self.f = function()
        self.bo = borrow()
        self.ba = back()
        self.h = history()
        self.o = organize()

    def main(self):
        self.m.initUI()
        self.m.s1.connect(self.show_func)

    def show_func(self):
        flag, resp = Login_in(self.m.e1.text(), self.m.e2.text())
        if flag:
            self.m.hide()
            self.f.s1.connect(self.show_bo)
            self.f.s2.connect(self.show_ba)
            self.f.s3.connect(self.show_his)
            self.f.s4.connect(self.show_org)
            self.f.s5.connect(self.Mb)
            self.f.initUI()
        else:
            msg = QMessageBox(QMessageBox.Warning, '系统提示', resp['message'])
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()

    def show_bo(self):
        self.f.hide()
        self.bo.s1.connect(self.Pb1)
        self.bo.s2.connect(self.borrow_)
        self.bo.initUI()

    def show_ba(self):
        self.f.hide()
        self.ba.s1.connect(self.Pb2)
        self.ba.s2.connect(self.back_)
        self.ba.initUI()

    def show_his(self):
        self.f.hide()
        self.h.s1.connect(self.Pb3)
        self.h.initUI()

    def show_org(self):
        self.f.hide()
        self.o.s1.connect(self.Pb4)
        self.o.s2.connect(self.org_)
        self.o.initUI()

    def Mb(self):
        self.f.close()
        self.m.initUI()

    def Pb1(self):
        self.bo.close()
        self.f.initUI()

    def Pb2(self):
        self.ba.close()
        self.f.initUI()

    def Pb3(self):
        self.h.close()
        self.f.initUI()

    def Pb4(self):
        self.o.close()
        self.f.initUI()

    def borrow_(self):
        url_ = 'manager/borrow'
        if self.bo.e1.text() != '' and self.bo.e2.text() != '':
            msg = QMessageBox(QMessageBox.Warning, '系统提示', transfer(self.bo.e1.text(), self.bo.e2.text(), url_))
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()
        else:
            msg = QMessageBox(QMessageBox.Warning, '系统提示', '空')
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()

    def back_(self):
        url_ = 'manager/returnBook'
        if self.ba.e1.text() != '' and self.ba.e2.text() != '':
            msg = QMessageBox(QMessageBox.Warning, '系统提示', transfer(self.ba.e1.text(), self.ba.e2.text(), url_))
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()
        else:
            msg = QMessageBox(QMessageBox.Warning, '系统提示', '空')
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()

    def org_(self):
        if self.o.e1.text() != '' and self.o.c1.currentText() != '':
            msg = QMessageBox(QMessageBox.Warning, '系统提示', transfer_(self.o.e1.text(), self.o.c1.currentText()))
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()
        else:
            msg = QMessageBox(QMessageBox.Warning, '系统提示', '空')
            msg.addButton('返回', QMessageBox.NoRole)
            msg.setWindowIcon(QIcon('Icon.png'))
            msg.exec_()


if __name__ == '__main__':
    constance.init()
    app = QApplication(sys.argv)
    c = Controller()
    c.main()
    sys.exit(app.exec_())
